package Demo1;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class editServlet extends HttpServlet {
    // 前端提交数据这里最好就是用post请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.setCharacterEncoding("UTF-8");
         resp.setContentType("text/html;charset=UTF-8");
         PrintWriter out = resp.getWriter();

         // 获取前端的数据
        String deptno = req.getParameter("deptno");

        // 连接数据库
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

out.println("        <!DOCTYPE html>");
out.println("<html lang='en'>");
out.println("<head>");
out.println("  <meta charset='UTF-8'>");
out.println("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.println("    <title>Edit</title>");
out.println("    <style>");
out.println("                table{");
out.println("            border: 2px solid orange;");
out.println("            margin:30px auto;");
out.println("            border-collapse: collapse;");
out.println("            text-align: center;");
out.println("     }");
out.println("        tr,td,th{");
out.println("            border: 2px solid orange;");
out.println("      }");
out.println("    </style>");
out.println("</head>");
out.println("<body>");
out.println("<hr>");





        try {

            connection  = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept where deptno = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,deptno);
            resultSet = preparedStatement.executeQuery();

            //这里的while循环只会执行一次
            while(resultSet.next()){
                String dno = resultSet.getString("deptno");
                String dname = resultSet.getString("dname");
                String loc = resultSet.getString("loc");

                out.println("<h1>修改部门</h1>");
                out.println("<form action='/oa/dept/modify' method='post'>");
                out.println("               部门号<input type='text' name='deptno' value='"+dno+"' readonly='readonly'><br>");
                out.println("               名称<input type='text' name='dname' value='"+dname+"'><br>");
                out.println("                工作地点<input type='text' name='loc' value='"+loc+"'><br>");
                out.println("   <input type='submit' value='保存'>");
                out.println("    <input type='button' value='后退' onclick='window.history.back()'>");
                out.println("</form>");

            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        out.println("</body>");
        out.println("</html>");

    }
}
